import config from 'config'
import httpFetch from 'share/httpFetch'
import errorMessage from 'share/errorMessage';
import dataSources from 'containers/my-account/data-sources/data-sources';
import { deepCopy, messages } from 'share/common';

export default {
  /**
   * 得到组件列表
   */
  getWidgetList(){
    return httpFetch.get(`${config.baseUrl}/invoice/api/expense/widgets`)
  },

  /**
   * 得到图标列表
   */
  getIconList(){
    return httpFetch.get(`${config.baseUrl}/api/expense/types/icon`)
  },

  /**
   * 排序大类
   * @param params  大类数组
   * @return {*|AxiosPromise}
   */
  sortCategory(params){
    return httpFetch.post(`${config.baseUrl}/invoice/api/expense/types/category/sort`, params)
  },

  /**
   * 创建大类
   * @param expenseTypeCategory
   */
  createCategory(expenseTypeCategory){
    return httpFetch.post(`${config.baseUrl}/api/expense/types/category`, expenseTypeCategory)
  },

  /**
   * 删除大类
   * @param expenseTypeCategoryOID
   */
  deleteCategory(expenseTypeCategoryOID){
    return httpFetch.delete(`${config.baseUrl}/api/expense/types/category/${expenseTypeCategoryOID}`)
  },

  /**
   * 编辑大类
   * @param expenseTypeCategory
   */
  editCategory(expenseTypeCategory){
    return httpFetch.put(`${config.baseUrl}/api/expense/types/category`, expenseTypeCategory)
  },

  /**
   * 排序小类
   * @param params  小类数组
   * @return {*|AxiosPromise}
   */
  sortExpenseType(params){
    return httpFetch.post(`${config.baseUrl}/invoice/api/expense/types/sort`, params)
  },

  /**
   * 获得费用类型编码
   */
  getExpenseTypeCode(setOfBooksId){
    return httpFetch.get(`${config.baseUrl}/invoice/api/expense/type/code/produce`, { setOfBooksId })
  },

  /**
   * 获得费用类型详情
   * @param id
   * @param copy
   */
  getExpenseTypeDetail(id, copy) {
    let url;
    copy ? url = '/api/expense/types/copy/' : url = '/api/expense/types/select/';
    return httpFetch.get(`${config.baseUrl}${url}${id}`);
  },

  /**
   * 保存费用类型
   *  @param expenseType
   * @param copy
   */
  saveExpenseType(expenseType, copy) {
    let url;
    copy ? url = '/api/expense/type/copy/base' : url = '/invoice/api/expense/type/base';
    return httpFetch.post(`${config.baseUrl}${url}`, expenseType);
  },

  /**
   * 保存费用类型的字段
   * @param expenseTypeId
   * @param fields
   * @return {*|AxiosPromise}
   */
  saveExpenseTypeFields(expenseTypeId, fields){
    return httpFetch.post(`${config.baseUrl}/invoice/api/expense/type/${expenseTypeId}/fields`, fields)
  },

  /**
   * 获得费用类型人员组权限
   * @param expenseTypeId
   */
  getExpenseTypeScope(expenseTypeId){
    return httpFetch.get(`${config.baseUrl}/invoice/api/expense/type/user/groups`, { expenseTypeId })
  },

  /**
   * 保存费用类型权限
   * @param scope
   * @return {*|AxiosPromise}
   */
  saveExpenseTypeScope(scope){
    return httpFetch.post(`${config.baseUrl}/invoice/api/expense/type/user/groups`, scope)
  },

  //判断组件能否删除
  checkFieldCanDelete(expenseTypeId, mappedColumnId) {
    return httpFetch.post(`${config.baseUrl}/invoice/api/expense/type/${expenseTypeId}/fields/check`, {mappedColumnId})
  },
  /**
   * 获取可关联的申请单类型
   * @returns {*}
   */
  getApplicationList(setOfBooksId) {
    const { relateApplicationTree } = dataSources;
    return new Promise((resolve, reject) => {
      httpFetch.get(`${config.baseUrl}/api/expense/type/form/query?setOfBooksId=${setOfBooksId}`)
        .then((response) => {
          let trees = deepCopy(relateApplicationTree);
          let expandedKeys = ['1-1', '2-1'];
          let { data } = response;
          data.rows && data.rows['companyForms'].forEach((item, index) => {
            if (item.formList[0]) {
              let children = {
                title: item.companyName,
                key: 'C' + '-' + item.companyId,
                selectable: false,
                children: []
              };
              item.formList.map(item => {
                children.children.push({
                  key: item.id,
                  title: item.formName + (!item.valid ? `(${messages('common.disabled')})` : ''),
                  disabled: !item.valid
                });
              });
              trees[1].children.push(children);
            } else {
              trees.splice(1, 1);
            }
          });

          data.rows && data.rows['setOfBooksForms'].forEach((item) => {
            if (item.formList[0]) {
              let children = {
                title: item.setOfBooksName,
                key: 'S' + '-' + item.setOfBooksId,
                selectable: false,
                children: []
              };
              item.formList.map(item => {
                children.children.push({
                  key: item.id,
                  title: item.formName + (!item.valid ? `(${messages('common.disabled')})` : ''),
                  disabled: !item.valid
                });
              });
              trees[0].children.push(children);
              expandedKeys.push('S' + '-' + item.setOfBooksId);
            } else {
              trees.splice(0, 1);
            }
          });
          resolve({ trees, expandedKeys });
        })
        .catch((err) => {
          errorMessage(err.response);
          reject(err.response);
        });
    });
  },

  //获取参与核算的列表
  getCheckList(setOfBooksId) {
    return httpFetch.get(`${config.baseUrl}/ledger/api/function/segment/query/custom/segment?setOfBooksId=${setOfBooksId}&type=1009`)
  },

  //是否显示参与核算的控件
  isShowCheckBox() {
    return httpFetch.get(`${config.baseUrl}/api/open/ledger/is/enable`)
  },

  //根据fieldType获取对应的可选择的messageKey
  getMessageKeyList(fieldType) {
    return httpFetch.get(`${config.baseUrl}/api/expense/type/keys`, {fieldType})
  },

}
